<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

if(!class_exists('api_360_recieve')){
    class Api_360_recieve extends CI_Controller {

        protected $_action = '';

        protected $_distance = 325;

        public function __construct(){
            parent::__construct();
            $this->load->model('Apilogmodel','m_api_log');
            $this->load->model('Usermodel','m_user');
            $this->load->model('Appmodel','m_app');
            $this->load->model('K360receivecustomer','m_customer');
            $this->load->model('configmodel','m_config');
            $this->load->database();

        }

        public function __destruct() {
            // $this->session->unset_userdata($this->_action);
            // //api开头的不存
            if(is_string($this->uri->rsegments[2]) && stripos($this->uri->rsegments[2], 'api') === 0) {
                $this->db->close();
                return;
            }
            $customer_id = $this->session->userdata('360_receive_customer_id');
            $info = $this->input->post();
            unset($info['customer_passport_picture_info_path']);
            unset($info['customer_passport_picture_visa_path']);
            $result = $this->response->getResult();
            @$this->m_api_log->createApiLog(array(
                'ip_address' => $this->input->ip_address(),
                'customer_id' => empty($customer_id) ? 0 :  $customer_id,
                'request_uri' => $_SERVER['REQUEST_URI'],
                'post_data' => json_encode($info,JSON_UNESCAPED_UNICODE),
                'result_data' => json_encode($result,JSON_UNESCAPED_UNICODE),
                'api_log_time_create' => NOW,
                'ua' => empty($_SERVER['HTTP_USER_AGENT']) ? '' : $_SERVER['HTTP_USER_AGENT'],
            ));

            $this->db->close();
        }


        public function submitInfo() {
            // $this->validate->setRule('gift_type','integer');
            $this->validate->setRule('customer_name','string');
            $this->validate->setRule('customer_name','message','请填写姓名');
            $this->validate->setRule('customer_phone','string');
            $this->validate->setRule('customer_phone','message','请填写国内联系电话');
            $this->validate->setRule('customer_address','string');
            $this->validate->setRule('customer_address','notnull',false);
            $this->validate->setRule('customer_address','optional',true);
            $this->validate->setRule('customer_weixin','string');
            $this->validate->setRule('customer_weixin','message','请填写微信号');
            $this->validate->setRule('customer_passport_picture_info_path','string');
            $this->validate->setRule('customer_passport_picture_info_path','message','请上传护照信息照片');
            if(!$this->validate->run($this->input->post(),true)){
                $this->response->setDetail($this->validate->getMessage());
                $this->response->responseExit();
            }
            $info = $this->validate->getResult();

            if(!preg_match("/^1[0-9]\d{9}$/i", $info['customer_phone'])){
                $this->response->setDetail('请输入正确的手机号码!');
                $this->response->responseExit();
            }

            if($this->m_customer->checkcusomerExisted($info['customer_phone'])) {
                $this->response->setDetail('该手机号已申请了!');
                $this->response->responseExit();
            }

            //保存base64字符串为图片
            //匹配出图片的格式
            if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $info['customer_passport_picture_info_path'], $result)){
              $type = $result[2];
              if($type == 'jpeg') {
                $type = 'jpg';
              }
              $this->m_app->initFolder();
              $filename = '360_receive_'.date('YmdHis')."_1.{$type}";
              $path = UPLOAD_PATH_ORIGIN.$filename;
              if (file_put_contents($path, base64_decode(str_replace($result[1], '', $info['customer_passport_picture_info_path'])))){
                    $info['customer_passport_picture_info_path'] = $filename;
              } else {
                $this->response->setDetail('图片保存失败!请重新上传!');
                $this->response->responseExit();
              }
            } else {
                $this->response->setDetail('图片格式上传错误!请重新上传!');
                $this->response->responseExit();
            }

            $info['customer_time_create'] = NOW;
            $info['customer_status'] = STATUS_ACTIVE;
            $customer_id = $this->m_customer->createCustomer($info);
            if($customer_id == 0) {
                $this->response->setDetail('提交失败!手机号已申请!');
                $this->response->responseExit();
            }
            $this->session->set_userdata('360_receive_customer_id',$customer_id);

            $this->response->setSuccess();
            $this->response->setDetail('提交成功!');
            $this->response->responseExit();
        }

    }
    
}